//
//  MSFoodHeadDetailView.m
//  MSJD
//
//  Created by xiangbo on 2018/9/28.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSFoodIntroDetailView.h"
#import "MSScoreView.h"
#import "MSLocationButton.h"
#import "NSString+Regex.h"
#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "MSCollectionBaseCell.h"

@interface MSFoodIntroDetailView ()<MSDetailViewDelegate,TYCyclePagerViewDelegate,TYCyclePagerViewDataSource>

@property (nonatomic, strong) TYCyclePagerView *bannerScrollView;
@property (nonatomic, strong) TYPageControl *pageControl;

@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UILabel *titleLabel;
//@property (nonatomic,strong)MSLocationButton *areaButton;

@property (nonatomic, strong)UIImageView *locImage;
@property (nonatomic, strong)UILabel *LocLabel;
@property (nonatomic,strong)MSScoreView *scoreView;


@property (nonatomic,strong)UIView *lineView;
@property (nonatomic,strong)UILabel *desLabel;
@property (nonatomic,strong)UILabel *openTimeLabel;
@property (nonatomic,strong)UILabel *contactDesLabel;
@property (nonatomic,strong)UILabel *contactPhoneLabel;
@property (nonatomic,strong)UIButton *contactButton;
@property (nonatomic,strong)MSFoodDetailData *detailData;

@end

@implementation MSFoodIntroDetailView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubviews];
    }
    return self;
}


- (void)setupSubviews{
    self.imageView = [[UIImageView alloc] init];
    self.imageView.backgroundColor = MSSubTitleColor;
    [self addSubview:self.imageView];
    
    [self addSubview:self.bannerScrollView];
    [self.bannerScrollView addSubview:self.pageControl];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"我是标题";
    self.titleLabel.textColor = [UIColor colorWithHexString:@"#262628"];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [self addSubview:self.titleLabel];
    
//    self.areaButton =[[MSLocationButton alloc] init];
//    [self.areaButton setTitle:@"欢乐海岸" forState:UIControlStateNormal];
//    [self addSubview:self.areaButton];
    self.locImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Shape"]];
    [self addSubview:self.locImage];
    
    self.LocLabel = [[UILabel alloc] init];
    self.LocLabel.font = [UIFont systemFontOfSize:13.f];
    self.LocLabel.textColor = MSSubTitleColor;
    self.LocLabel.text = @"欢乐海岸";
    [self addSubview:self.LocLabel];
    
    self.scoreView =[[MSScoreView alloc] init];
    [self addSubview:self.scoreView];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    [self addSubview:self.lineView];
    
    UIView *contactView = [[UIView alloc] init];
    [self addSubview:contactView];
    
    self.desLabel = [[UILabel alloc] init];
    self.desLabel.font = [UIFont systemFontOfSize:15.f];
    self.desLabel.text = @"营业中:";
    self.desLabel.textColor = MSSubTitleColor;
    [contactView addSubview:self.desLabel];
    
    self.openTimeLabel = [[UILabel alloc] init];
    self.openTimeLabel.font = [UIFont systemFontOfSize:15.f];
    self.openTimeLabel.text = @"10:00-23:00";
    self.openTimeLabel.textColor = [UIColor colorWithHexString:@"#4A4A4A"];
    [contactView addSubview:self.openTimeLabel];
    
    self.contactDesLabel = [[UILabel alloc] init];
    self.contactDesLabel.font = [UIFont systemFontOfSize:15.f];
    self.contactDesLabel.text = @"联系电话:";
    self.contactDesLabel.textColor = MSSubTitleColor;
    [contactView addSubview:self.contactDesLabel];
    
    self.contactPhoneLabel = [[UILabel alloc] init];
    self.contactPhoneLabel.font = [UIFont systemFontOfSize:15.f];
    self.contactPhoneLabel.textColor = [UIColor colorWithHexString:@"#4A4A4A"];
    [contactView addSubview:self.contactPhoneLabel];
    
    self.contactButton =[UIButton buttonWithType:UIButtonTypeCustom];
    self.contactButton.layer.cornerRadius = 22.f;
    [self.contactButton setImage:[UIImage imageNamed:@"phone"] forState:UIControlStateNormal];
    [self.contactButton addTarget:self action:@selector(contactButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [contactView addSubview:self.contactButton];
    
    UIView *spaceView =[[UIView alloc] init];
    spaceView.backgroundColor = UIColorHex(0xF2F2F2);
    [self addSubview:spaceView];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(self.imageView.mas_width).multipliedBy(280 / 375.f);
    }];
    
    [self.bannerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(self.imageView.mas_width).multipliedBy(280 / 375.f);
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bannerScrollView).offset(-10);
        make.left.equalTo(self.bannerScrollView);
        make.right.equalTo(self.bannerScrollView);
        make.height.mas_equalTo(2);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(8);
        make.left.equalTo(self).offset(14);
        make.centerX.equalTo(self);
    }];
    
    [self.scoreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self);
        make.height.mas_equalTo(30);
        make.top.equalTo(self.titleLabel.mas_bottom);
    }];
    
    [self.locImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scoreView.mas_bottom).offset(3);
        make.left.equalTo(self.scoreView);
        make.width.mas_equalTo(self.locImage.width);
        make.height.mas_equalTo(self.locImage.height);
    }];
    
    [self.LocLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.locImage.mas_right).offset(6);
        make.right.equalTo(self).offset(-10);
        make.centerY.equalTo(self.locImage);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.locImage.mas_bottom).offset(7);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    [contactView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(66);
    }];
    
    [self.contactButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contactView.mas_centerY);
        make.right.equalTo(contactView).offset(-8);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
    }];
    
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(contactView.mas_centerY).offset(-5);
        make.left.equalTo(contactView).offset(14);
        make.width.mas_equalTo(60);;
    }];
    
    [self.openTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.desLabel.mas_right).offset(3);
        make.centerY.equalTo(self.desLabel);
        make.right.equalTo(self.contactButton.mas_left).offset(-7);
    }];
    
    [self.contactDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contactView.mas_centerY).offset(3);
        make.left.equalTo(self.desLabel);
    }];
    
    [self.contactPhoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contactDesLabel.mas_right).offset(3);
        make.centerY.equalTo(self.contactDesLabel);
        make.right.equalTo(self.openTimeLabel);
    }];
    
    [spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contactView.mas_bottom);
        make.left.right.and.bottom.equalTo(self);
    }];
}

- (void)contactButtonAction:(id)sender{
    if ([self.contactPhoneLabel.text isTelephone]) {
        NSMutableString * str = [[NSMutableString alloc] initWithFormat:@"tel:%@",self.contactPhoneLabel.text];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self addSubview:callWebview];
    }
    
}

- (void)configData:(id)data indexPath:(NSIndexPath *)indexPath{
    self.detailData = (MSFoodDetailData *)data;
    [self.imageView setImageWithURL:[NSURL URLWithString:self.detailData.img]  placeholder:[UIImage imageWithColor:MSPlaceholderColor]];
    self.titleLabel.text = self.detailData.itemName;
//    [self.areaButton setTitle:detailData.location forState:UIControlStateNormal];
    self.LocLabel.text = self.detailData.location;
    self.scoreView.score = self.detailData.score;
    self.openTimeLabel.text = self.detailData.openTime;
    self.imageView.hidden = self.detailData.covers.count > 0 ? YES : NO;
    self.bannerScrollView.hidden = !self.imageView.hidden;
    self.contactPhoneLabel.text = self.detailData.phone;
    self.pageControl.numberOfPages = self.detailData.covers.count;
    [self.bannerScrollView reloadData];
    
}

#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.detailData.covers.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    if (index >= self.detailData.covers.count) {
        return [UICollectionViewCell new];
    }
    NSString *imageUrl = self.detailData.covers[index];
    MSPhotosCollectionViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(MSPhotosCollectionViewCell.class)  forIndex:index];
    [cell.image setImageURL:[NSURL URLWithString:imageUrl]];
    return cell;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
    self.pageControl.currentPage = toIndex;
}
#pragma mark - Getters

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    CGFloat space = 0;
    CGSize itemSize = self.bannerScrollView.size;
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = itemSize;
    layout.itemSpacing = space;
    return layout;
}

- (TYCyclePagerView *)bannerScrollView {
    if (!_bannerScrollView) {
        _bannerScrollView = [[TYCyclePagerView alloc] init];
        _bannerScrollView.reloadDataNeedResetIndex = YES;
        [_bannerScrollView registerClass:MSPhotosCollectionViewCell.class forCellWithReuseIdentifier:NSStringFromClass(MSPhotosCollectionViewCell.class)];
        _bannerScrollView.autoScrollInterval = 5;
        _bannerScrollView.delegate = self;
        _bannerScrollView.dataSource = self;
    }
    return _bannerScrollView;
}

- (TYPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[TYPageControl alloc] init];
        [_pageControl setHidesForSinglePage:NO];
        //        _pageControl.pageIndicatorTintColor = UIColorHex(0xdddddd);
        _pageControl.pageIndicatorImage = [UIImage imageWithColor:UIColorHex(0xdddddd) size:CGSizeMake(8, 2)];
        //        _pageControl.currentPageIndicatorTintColor = UIColorHex(0xF96332);
        _pageControl.currentPageIndicatorImage = [UIImage imageWithColor:UIColorHex(0xF96332) size:CGSizeMake(8, 2)];
        _pageControl.pageIndicatorSize = CGSizeMake(8, 2);
    }
    return _pageControl;
}

+ (CGFloat)headDetailViewHeight{
    CGFloat height = MS_SCREEN_WIDTH * 280 / 375;
    return height + 160;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
